/* stores the Temperature at every location in the simulation*/
import java.util.Random;
public class TempMap
{
 static int[][] tmap; //intenal representation of temperature map

 static int atmo_bleed=1;//how much is heat is lost ever moment/
 public static int[][] GetTempData()
 {
   // returns raw tempmap for the renderer
   return tmap;
 }
 public static void SetTempData(int[][] map) {
   tmap = map;
 }
 
 public static int getTempHere(loc L)
 {
  return tmap[L.x][L.y]; 
 }
 public static void Initalize()
 {
   Random R = new Random();
  //Initalize all temperature values with a gradient

   tmap = new int[SimulationConstants.simsize][SimulationConstants.simsize];
   for(int x=0; x<SimulationConstants.simsize; x++)
   {
     for(int y=0; y<SimulationConstants.simsize; y++)
     {
       tmap[x][y] = (int)(Math.cos(Math.PI*((double)(y)-(double)(SimulationConstants.simsize/2))/(double)(SimulationConstants.simsize))*80.0+20);

     }
   }
 }
   
   
 
  public static void ModifyTemp(loc[][] wmap)
{
    Magic_Eight_Ball.prove_TempMap_Updates(); //This code is here for testing purposes
 //modify map using wind 
    //make a copy of tmap
    Random R = new Random();
    int thistotal=0;//a running total of heat this cycle
    int[][] ctmap = new int[SimulationConstants.simsize][SimulationConstants.simsize];
    for(int x=0; x<SimulationConstants.simsize; x++)
    {
      for(int y=0; y<SimulationConstants.simsize; y++)
      {
        ctmap[x][y]=0;
      }
    }
    int windUpdates =0;//debug
    for(int x=0; x<SimulationConstants.simsize; x++)
    {
      for(int y=0; y<SimulationConstants.simsize; y++)
      {
       
        int newx = (x + wmap[x][y].x)%SimulationConstants.simsize;
        int newy = (y + wmap[x][y].y)%SimulationConstants.simsize;
        if(newx<0)
          newx+=SimulationConstants.simsize;
        if(newy<0)
          newy+=SimulationConstants.simsize;
        double amount = Math.pow(((double) wmap[x][y].x*wmap[x][y].x + wmap[x][y].y*wmap[x][y].y),-0.5);
       if(wmap[x][y].x!=0||wmap[x][y].y!=0)
       {
         windUpdates++;//keep track of how many times wind has actually mattered
        int pulled = tmap[x][y]/2;
        tmap[x][y]-=pulled;
        ctmap[newx][newy]+=pulled;
        //if(amount>0.0)//debug
         // //////System.out.println(amount);//debug
          //windUpdates--;
       }
       else
         ctmap[x][y]=0;
      }
    }
    //////System.out.println(windUpdates+" wind updates");//debug
    double averagetemp =0.0;
    for(int x=0; x<SimulationConstants.simsize; x++)
    {
      for(int y=0; y<SimulationConstants.simsize; y++)
      {
        tmap[x][y]+=ctmap[x][y] +(int)(Math.cos(Math.PI*((double)(y)-(double)(SimulationConstants.simsize/2))/(double)(SimulationConstants.simsize))*(SimulationConstants.sunlight*10)-(5-SimulationConstants.sunlight*5));
        if(tmap[x][y]<-100)
          tmap[x][y]= -100;//atmosphere cannot get much colder then this
       averagetemp+=tmap[x][y]; 
      }
    }
    averagetemp/=SimulationConstants.simsize*SimulationConstants.simsize;
 //   System.out.println(averagetemp);

}
  public static void SeismicTemp(loc s)
  {
   //set temp due to seismic event 
   for(int r1=-7; r1<=7; r1++)
   {
     for(int r2=-7; r2<=7; r2++)
     {
       tmap[((s.x+r1)%512<0)?(512+(s.x+r1)%512):(s.x+r1)%512][((s.y+r2)%512<0)?(512+(s.y+r2)%512):(s.y+r2)%512]=1000;//The trinary operators deal with wrapping around the left and bottom edges
     }
   }
    
   
    
  }
  
}
